/*
 ============================================================================
 Name        : RekursiveMap.c
 Author      : 
 Version     :
 Copyright   : Your copyright notice
 Description : Hello World in C, Ansi-style
 ============================================================================
 */

#include <stdio.h>
#include <stdlib.h>
#include "StringMap.h"
#include <stdbool.h>
#include <string.h>

Str_Map sm;
Str_Map sm1;
Str_Map sm_new;

void put(String key, Object obj) {
	if (contains(key) == 1)
		printf("element mit dem key schon vorhanden\n");
	else {
		sm_new = (Str_Map) malloc(sizeof(struct string_map_struct));

		sm_new->key = key;
		sm_new->obj = obj;

		sm_new->next = sm->next;
		sm->next = sm_new;

	}
}

Object get(String key) {

	sm1 = sm;
	Object o = NULL;

	while (sm1->next != NULL ) {

		if (strcmp(key, sm1->next->key) == 0) {
			o = sm1->next->obj;
		}
		sm1 = sm1->next;
	}
	return o;
}

Object remuve(String key) {

	sm1 = sm;
	Object o = NULL;

	while (sm1->next != NULL ) {

		if (strcmp(key, sm1->next->key) == 0) {

			sm1->next = sm1->next->next;
			return o;
		}

		sm1 = sm1->next;
	}
	return o;
}

int contains(String key) {

	sm1 = sm;
	while (sm1->next != NULL ) {

		if (strcmp(key, sm1->next->key) == 0) {
			sm1 = sm1->next;
			return 1;
		}
		sm1 = sm1->next;
	}
	return 0;
}

int main(void) {

	sm = (Str_Map) malloc(sizeof(struct string_map_struct));

	sm->next = NULL;

	put("1", "obj1");
	put("2", "obj2");
	put("3", "obj3");
	put("4", "obj4");
	put("5", "obj5");

	printf("vor remuve\n");

	printf("get ruckgabe1 = %s\n", get("1"));
	printf("get ruckgabe2 = %s\n", get("2"));
	printf("get ruckgabe3 = %s\n", get("3"));
	printf("get ruckgabe4 = %s\n", get("4"));
	printf("get ruckgabe5 = %s\n", get("5"));

	remuve("2");
	remuve("4");

	printf("nach remuve\n\n");

	printf("get ruckgabe1 = %s\n", get("1"));
	printf("get ruckgabe2 = %s\n", get("2"));
	printf("get ruckgabe3 = %s\n", get("3"));
	printf("get ruckgabe4 = %s\n", get("4"));
	printf("get ruckgabe5 = %s\n", get("5"));


	free(sm);
	free(sm_new);
	return EXIT_SUCCESS;
}

